package com.yxy.controller.user;

import com.yxy.common.PageResult;
import com.yxy.common.Result;
import com.yxy.constant.MessageConstant;
import com.yxy.constant.RequestMapperConstant;
import com.yxy.exception.QRCodeGeneratorException;
import com.yxy.pojo.dto.SightsBookingDTO;
import com.yxy.pojo.dto.SightsBookingPageQueryDTO;
import com.yxy.pojo.vo.SightsBookingVO;
import com.yxy.service.SightsBookingService;
import com.yxy.utils.QRCodeGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

@RestController("userSightsBookingController")
@RequestMapping(RequestMapperConstant.USER_REQUEST_MAPPER + "sightsBooking")
@Slf4j
public class SightsBookingController {

    @Autowired
    private SightsBookingService sightsBookingService;

    // 对应前端页面跳转地址
    public static final String URL = "http://tourism.ymvpear.icu/#/sights/booking/qr/";

    /**
     * 添加景点预定信息
     * @param sightsBookingDTO
     * @return
     */
    @PostMapping("/addSightsBooking")
    public Result addSightsBooking(@RequestBody SightsBookingDTO sightsBookingDTO) {
        log.info("用户添加景点预定信息：{}", sightsBookingDTO);
        sightsBookingService.addSightsBooking(sightsBookingDTO);
        return Result.success("预约成功");
    }

    /**
     * 获取景点预定信息
     * @param id
     * @return
     */
    @GetMapping("/getSightsBooking")
    public Result<SightsBookingVO> getSightsBooking(Integer sightsId, Integer userId) {
        log.info("获取用户景点预定信息：景点id：{}，用户id：{}", sightsId, userId);
        SightsBookingVO vo = sightsBookingService.getSightsBooking(sightsId, userId);
        return Result.success("", vo);
   }

    /**
     * 取消景点预定信息
     * @param id
     * @return
     */
    @PutMapping("/cancelSightsBooking/{bookingId}/{sightsId}/{userId}")
   public Result cancelSightsBooking(@PathVariable Integer sightsId, @PathVariable Integer userId, @PathVariable Integer bookingId) {
        log.info("取消用户景点预定信息：景点id：{}，用户id：{}, 预约id，{}", sightsId, userId, bookingId);
        sightsBookingService.cancelSightsBooking(sightsId, userId, bookingId);
        return Result.success("取消成功");
    }

    /**
     * 分页查询用户景点预定信息
     * @param sightsBookingPageQueryDTO
     * @return
     */
    @GetMapping("/getSightsBookingList")
    public Result<PageResult> pageQuerySightsBooking(SightsBookingPageQueryDTO sightsBookingPageQueryDTO){
        log.info("分页查询用户景点预定信息：{}", sightsBookingPageQueryDTO);
        PageResult pageResult = sightsBookingService.pageQuerySightsBooking(sightsBookingPageQueryDTO);
        return Result.success("", pageResult);
    }

    /**
     * 获取用户景点预定信息
     * @param id
     * @return
     */
    @GetMapping("/getSightsBookingById")
    public Result<SightsBookingVO> getSightsBookingById(Integer id){
        log.info("获取用户景点预定信息：{}", id);
        SightsBookingVO vo = sightsBookingService.getSightsBookingById(id);
        return Result.success("", vo);
    }

    /**
     * 使用景点预定信息
     * @param id
     * @return
     */
    @PutMapping("/useSightsBooking/{id}")
    public Result<String> use(@PathVariable int id){
        log.info("使用景点预定信息：{}", id);
        String status = sightsBookingService.use(id);
        return Result.success("", status);
    }

    /**
     * 生成二维码
     * @param id
     * @return
     */
    @GetMapping("/getQRCode")
    public Result QRCode(Integer id){
        log.info("生成景点预约二维码：{}", id);
        String base64 = "";
        try {
            String path = QRCodeGenerator.generateQRCodeImage(URL + id, 100, 100, id, QRCodeGenerator.SIGHTS);
            // 转成Base64
            byte[] imageBytes = Files.readAllBytes(Paths.get(path));
            base64 = Base64.getEncoder().encodeToString(imageBytes);
        } catch (Exception e) {
            throw new QRCodeGeneratorException(MessageConstant.QR_CODE_GENERATION_FAILURE);
        }
        return Result.success("", base64);
    }
}
